/*
 *  $Id: Button.cs 609 2009-07-09 16:30:09Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2009 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using AgateLib.Geometry;

namespace Ragima.UI {
    /// <summary>
    /// Состояния кнопки.
    /// </summary>
    public enum ButtonState {
        Normal,
        Hovered,
        Pressed,
        Disabled,
    }

    /// <summary>
    /// Абстрактный класс кнопки с четырьмя состояниями и событием.
    /// </summary>
    public abstract class Button: Region {
        private bool _down, _enabled;
        private SimpleEvent _action;

        public Button(Rectangle rect, SimpleEvent action): base(rect) {
            _down = false;
            _enabled = true;
            _action = action;
        }

        public bool Down {
            get { return _down; }
        }

        public bool Enabled {
            get { return _enabled; }
            set {
                _enabled = value;
                if (!_enabled) _down = false;
            }
        }

        public ButtonState State {
            get {
                if (!_enabled) return ButtonState.Disabled;
                if (_down) return ButtonState.Pressed;
                return Focus ? ButtonState.Hovered : ButtonState.Normal;
            }
        }

        public override void OnClick() {
            if (_enabled)
                _down = true;
        }
        public override void OnClickUp() {
            if (_down) {
                _down = false;
                if (_enabled && _action != null)
                    _action();
            }
        }
        public override void OnMouseLeave() {
            base.OnMouseLeave();
            _down = false;
        }

        public override void Draw() {
            switch (State) {
                case ButtonState.Normal: DrawButtonNormal(); break;
                case ButtonState.Hovered: DrawButtonHovered(); break;
                case ButtonState.Pressed: DrawButtonPressed(); break;
                case ButtonState.Disabled: DrawButtonDisabled(); break;
            }
        }

        public abstract void DrawButtonNormal();
        public abstract void DrawButtonHovered();
        public abstract void DrawButtonPressed();
        public abstract void DrawButtonDisabled();
    }
}
